package cn.edu.imust.loginregisterdemo.home.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.ButterKnife;
import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.mine.view.MineFragment;
import cn.edu.imust.loginregisterdemo.upload.view.UploadFragment;

public class MainActivity extends AppCompatActivity  {

    private HomeFragment    homeFragment;
    private UploadFragment  uploadFragment;
    private MineFragment    mineFragment;
    private FragmentManager fm;
    private int whatFragment = 0;//判断当前是哪一个fragment显示, 0:homeFragment;1:uploadFragment;2:mineFragment

    BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initListener();
        initFragment();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @SuppressLint("ResourceType")
    private void initView() {

        bottomNavigationView = this.findViewById(R.id.main_navigation_bar);
        HomeFragment homeFragment = new HomeFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.main_page_container, homeFragment);
        ft.commit();
        whatFragment = 0;

    }

    private void initListener() {
        bottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.home:
                    switch (whatFragment) {
                        case 0:
                            break;
                        case 1:
                            switchFragment(uploadFragment, homeFragment);
                            whatFragment = 0;
                            break;
                        case 2:
                            switchFragment(mineFragment, homeFragment);
                            whatFragment = 0;
                    }
                    Log.d("MainActivity", "切换到首页");
                    break;
                case R.id.upload:
                    switch (whatFragment) {
                        case 0:
                            switchFragment(homeFragment, uploadFragment);
                            whatFragment = 1;
                            break;
                        case 1:
                            break;
                        case 2:
                            switchFragment(mineFragment, uploadFragment);
                            whatFragment = 1;
                    }
                    Log.d("MainActivity", "切换到上传");
                    break;
                case R.id.mine:
                    switch (whatFragment) {
                        case 0:
                            switchFragment(homeFragment, mineFragment);
                            whatFragment = 2;
                            break;
                        case 1:
                            switchFragment(uploadFragment, mineFragment);
                            whatFragment = 2;
                            break;
                        case 2:
                            break;
                    }
                    Log.d("MainActivity", "切换到我的");
                    break;
                default:
                    break;
            }

            Log.d("MainActivity", "title==>" + menuItem.getTitle() + "id==>" + menuItem.getItemId());
            return true;
        });

    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        uploadFragment = new UploadFragment();
        mineFragment = new MineFragment();
        fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.main_page_container, homeFragment).commit();
    }

//    private void switchFragment(Fragment targetFragment) {
//        FragmentTransaction ft = fm.beginTransaction();
//        ft.replace(R.id.main_page_container, targetFragment);
//        ft.commit();
//    }

    public void switchFragment(Fragment from, Fragment to) {
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from)
                        .add(R.id.main_page_container, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
}
